<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>类式组件state</title>
</head>
<body>
<div id="app"></div>
<script src="../lib/babel.min.js"></script>
<script src="../lib/react.development.js"></script>
<script src="../lib/react-dom.development.js"></script>
<script type="text/babel">
  class Weather extends React.Component {
    constructor(props) {
      super(props);
      // 构造方法中初始化state
      this.state = {
        isHot: false
      }
      this.changeHot = this.changeHot.bind(this); // 事件处理的回调要绑定this,不然拿不到state
    }
    render() {
      return (
        <h2 onClick={this.changeHot}>今天天气很{this.state.isHot?'Hot':'cold'}</h2>
      )
    }
    changeHot() {
      const {isHot} = this.state
      // 状态必须通过setState来更改，更新的动作是一种合并，不是替换
      this.setState({
        isHot:!isHot
      })
    }
  }
  ReactDOM.render(<Weather/>, document.getElementById('app'))
</script>
</body>
</html>
